<template>
  <div class="recharge-container">
    <div class="recharge-header">
      <h1 class="recharge-title">账户充值</h1>
      <p>为您的账户充值，享受更多优质服务</p>
    </div>

    <div class="recharge-content">
      <!-- 账户余额卡片 -->
      <div class="balance-card">
        <h3>当前账户余额</h3>
        <div class="balance-amount">¥{{ userBalance.toFixed(2) }}</div>
        <p class="balance-tips">充值金额将实时到账</p>
      </div>

      <!-- 充值金额选择 -->
      <div class="amount-section">
        <h3>选择充值金额</h3>
        <div class="amount-options">
          <button
            v-for="amount in rechargeAmounts"
            :key="amount.value"
            class="amount-btn"
            :class="{ active: selectedAmount === amount.value }"
            @click="selectedAmount = amount.value"
          >
            <span class="amount-value">¥{{ amount.value }}</span>
            <span v-if="amount.bonus > 0" class="amount-bonus">+{{ amount.bonus }}元</span>
          </button>
        </div>
        
        <!-- 自定义金额输入 -->
        <div class="custom-amount">
          <label for="customAmount">自定义金额</label>
          <input
            type="number"
            id="customAmount"
            v-model.number="customAmount"
            placeholder="请输入充值金额"
            min="1"
            @input="handleCustomAmount"
          />
        </div>
      </div>

      <!-- 充值方式 -->
      <div class="payment-section">
        <h3>选择支付方式</h3>
        <div class="payment-options">
          <div
            v-for="payment in paymentMethods"
            :key="payment.id"
            class="payment-option"
            :class="{ active: selectedPayment === payment.id }"
            @click="selectedPayment = payment.id"
          >
            <div class="payment-icon">{{ payment.icon }}</div>
            <div class="payment-info">
              <div class="payment-name">{{ payment.name }}</div>
              <div class="payment-desc">{{ payment.description }}</div>
            </div>
            <div class="payment-check" v-if="selectedPayment === payment.id">✓</div>
          </div>
        </div>
      </div>

      <!-- 优惠券选择 -->
      <div class="coupon-section">
        <h3>选择优惠券</h3>
        <div class="coupon-selector">
          <div class="coupon-info">
            <div v-if="selectedCoupon" class="coupon-details">
              <span class="coupon-value">{{ selectedCoupon.discount }}折</span>
              <span class="coupon-desc">{{ selectedCoupon.description }}</span>
            </div>
            <div v-else class="no-coupon">暂无可用优惠券</div>
          </div>
          <button class="coupon-btn" @click="showCouponList = !showCouponList">
            选择优惠券
          </button>
        </div>
        
        <!-- 优惠券列表弹窗 -->
        <div v-if="showCouponList" class="coupon-list">
          <div
            v-for="coupon in availableCoupons"
            :key="coupon.id"
            class="coupon-item"
            @click="selectCoupon(coupon)"
          >
            <div class="coupon-item-discount">{{ coupon.discount }}折</div>
            <div class="coupon-item-info">
              <div class="coupon-item-name">{{ coupon.name }}</div>
              <div class="coupon-item-desc">{{ coupon.description }}</div>
              <div class="coupon-item-expire">有效期至: {{ formatDate(coupon.expireDate) }}</div>
            </div>
          </div>
        </div>
      </div>

      <!-- 订单摘要 -->
      <div class="order-summary">
        <div class="summary-item">
          <span>充值金额</span>
          <span>¥{{ finalAmount.toFixed(2) }}</span>
        </div>
        <div class="summary-item" v-if="selectedCoupon">
          <span>优惠折扣</span>
          <span class="discount">-¥{{ discountAmount.toFixed(2) }}</span>
        </div>
        <div class="summary-total">
          <span>实付金额</span>
          <span class="total-amount">¥{{ totalPayment.toFixed(2) }}</span>
        </div>
      </div>

      <!-- 充值按钮 -->
      <button class="recharge-btn" @click="handleRecharge" :disabled="loading">
        {{ loading ? '充值中...' : `确认充值 ¥${totalPayment.toFixed(2)}` }}
      </button>
    </div>

    <!-- 充值成功提示 -->
    <div v-if="showSuccess" class="success-modal">
      <div class="success-content">
        <div class="success-icon">✓</div>
        <h3>充值成功</h3>
        <p>您的账户已成功充值 ¥{{ finalAmount.toFixed(2) }}</p>
        <button class="success-btn" @click="showSuccess = false">完成</button>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, computed, onMounted } from 'vue';
import { useRouter } from 'vue-router';
import { useRechargeStore } from '../stores/recharge';

const router = useRouter();
const rechargeStore = useRechargeStore();

// 响应式数据
const selectedAmount = ref(0);
const customAmount = ref('');
const selectedPayment = ref('alipay');
const selectedCoupon = ref(null);
const showCouponList = ref(false);
const showSuccess = ref(false);
const loading = ref(false);

// 预设充值金额选项
const rechargeAmounts = [
  { value: 10, bonus: 0 },
  { value: 50, bonus: 5 },
  { value: 100, bonus: 15 },
  { value: 200, bonus: 35 },
  { value: 500, bonus: 100 },
  { value: 1000, bonus: 250 }
];

// 支付方式
const paymentMethods = [
  { id: 'alipay', name: '支付宝', description: '推荐使用支付宝支付', icon: '💳' },
  { id: 'wechat', name: '微信支付', description: '使用微信扫码支付', icon: '💚' },
  { id: 'creditcard', name: '银行卡', description: '支持储蓄卡/信用卡', icon: '🏦' },
  { id: 'unionpay', name: '银联云闪付', description: '银联快速支付', icon: '✨' }
];

// 可用优惠券
const availableCoupons = [
  { id: 1, name: '新用户专享', discount: 9, description: '新用户首次充值享受9折优惠', expireDate: new Date('2024-12-31') },
  { id: 2, name: '周末特惠', discount: 8.5, description: '周末充值享受8.5折优惠', expireDate: new Date('2024-11-30') }
];

// 计算属性
const userBalance = computed(() => rechargeStore.userBalance);

const finalAmount = computed(() => {
  const amount = selectedAmount.value || (customAmount.value ? Number(customAmount.value) : 0);
  // 查找对应的赠送金额
  const selectedOption = rechargeAmounts.find(option => option.value === selectedAmount.value);
  return amount + (selectedOption?.bonus || 0);
});

const discountAmount = computed(() => {
  if (!selectedCoupon.value || !finalAmount.value) return 0;
  return finalAmount.value * (1 - selectedCoupon.value.discount / 10);
});

const totalPayment = computed(() => {
  return Math.max(0, finalAmount.value - discountAmount.value);
});

// 方法
const handleCustomAmount = () => {
  if (customAmount.value) {
    selectedAmount.value = 0; // 清除预设选项
  }
};

const selectCoupon = (coupon) => {
  selectedCoupon.value = coupon;
  showCouponList.value = false;
};

const handleRecharge = async () => {
  if (!totalPayment.value || loading.value) return;

  loading.value = true;
  try {
    // 调用充值store进行充值
    await rechargeStore.recharge(totalPayment.value, finalAmount.value, selectedPayment.value, selectedCoupon.value);
    // 显示成功提示
    showSuccess.value = true;
    // 重置表单
    resetForm();
  } catch (error) {
    console.error('充值失败:', error);
    alert('充值失败，请稍后重试');
  } finally {
    loading.value = false;
  }
};

const resetForm = () => {
  selectedAmount.value = 0;
  customAmount.value = '';
  selectedCoupon.value = null;
};

const formatDate = (date) => {
  if (!date) return '';
  return new Date(date).toLocaleDateString('zh-CN');
};

// 生命周期
onMounted(() => {
  // 初始化时加载用户余额
  rechargeStore.fetchUserBalance();
});
</script>

<style scoped>
.recharge-container {
  max-width: 1000px;
  margin: 0 auto;
  padding: 20px;
}

.recharge-header {
  text-align: center;
  margin-bottom: 30px;
}

.recharge-header h1 {
  font-size: 28px;
  color: #333;
  margin-bottom: 10px;
}

.recharge-title {
  font-size: 36px;
  font-weight: bold;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
  text-fill-color: transparent;
  text-shadow: 0 4px 8px rgba(102, 126, 234, 0.2);
  letter-spacing: 2px;
  animation: titleGlow 2s ease-in-out infinite alternate;
}

@keyframes titleGlow {
  from {
    text-shadow: 0 4px 8px rgba(102, 126, 234, 0.2);
  }
  to {
    text-shadow: 0 6px 16px rgba(102, 126, 234, 0.4), 0 0 20px rgba(118, 75, 162, 0.3);
  }
}

.recharge-header p {
  font-size: 16px;
  color: #666;
}

.recharge-content {
  background-color: #fff;
  border-radius: 12px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
  padding: 30px;
}

/* 余额卡片 */
.balance-card {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  padding: 25px;
  border-radius: 12px;
  margin-bottom: 30px;
  text-align: center;
}

.balance-card h3 {
  margin: 0 0 15px 0;
  font-size: 18px;
  opacity: 0.9;
}

.balance-amount {
  font-size: 36px;
  font-weight: bold;
  margin-bottom: 10px;
}

.balance-tips {
  margin: 0;
  font-size: 14px;
  opacity: 0.8;
}

/* 充值金额选择 */
.amount-section,
.payment-section,
.coupon-section {
  margin-bottom: 30px;
}

.amount-section h3,
.payment-section h3,
.coupon-section h3 {
  font-size: 20px;
  color: #333;
  margin-bottom: 20px;
  padding-bottom: 10px;
  border-bottom: 2px solid #f0f0f0;
}

.amount-options {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
  gap: 15px;
  margin-bottom: 20px;
}

.amount-btn {
  padding: 20px;
  border: 2px solid #e0e0e0;
  border-radius: 8px;
  background-color: white;
  cursor: pointer;
  transition: all 0.3s ease;
  text-align: center;
}

.amount-btn:hover {
  border-color: #667eea;
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(102, 126, 234, 0.2);
}

.amount-btn.active {
  border-color: #667eea;
  background-color: rgba(102, 126, 234, 0.1);
}

.amount-value {
  font-size: 20px;
  font-weight: bold;
  color: #333;
  display: block;
}

.amount-bonus {
  font-size: 14px;
  color: #ff6b6b;
  margin-top: 5px;
  display: block;
}

.custom-amount {
  margin-top: 20px;
}

.custom-amount label {
  display: block;
  margin-bottom: 8px;
  color: #666;
}

.custom-amount input {
  width: 100%;
  padding: 12px;
  border: 2px solid #e0e0e0;
  border-radius: 8px;
  font-size: 16px;
  transition: border-color 0.3s ease;
}

.custom-amount input:focus {
  outline: none;
  border-color: #667eea;
}

/* 支付方式 */
.payment-options {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
  gap: 15px;
}

.payment-option {
  display: flex;
  align-items: center;
  padding: 20px;
  border: 2px solid #e0e0e0;
  border-radius: 8px;
  cursor: pointer;
  transition: all 0.3s ease;
  position: relative;
}

.payment-option:hover {
  border-color: #667eea;
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(102, 126, 234, 0.2);
}

.payment-option.active {
  border-color: #667eea;
  background-color: rgba(102, 126, 234, 0.1);
}

.payment-icon {
  font-size: 32px;
  margin-right: 15px;
}

.payment-info {
  flex: 1;
}

.payment-name {
  font-size: 18px;
  font-weight: bold;
  color: #333;
  margin-bottom: 5px;
}

.payment-desc {
  font-size: 14px;
  color: #666;
}

.payment-check {
  width: 24px;
  height: 24px;
  border-radius: 50%;
  background-color: #667eea;
  color: white;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 16px;
}

/* 优惠券 */
.coupon-selector {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 20px;
  border: 2px solid #e0e0e0;
  border-radius: 8px;
  cursor: pointer;
  transition: border-color 0.3s ease;
}

.coupon-selector:hover {
  border-color: #667eea;
}

.coupon-info {
  flex: 1;
}

.coupon-details {
  display: flex;
  align-items: center;
}

.coupon-value {
  font-size: 20px;
  font-weight: bold;
  color: #ff6b6b;
  margin-right: 10px;
}

.coupon-desc {
  font-size: 16px;
  color: #333;
}

.no-coupon {
  color: #999;
}

.coupon-btn {
  padding: 10px 20px;
  background-color: #667eea;
  color: white;
  border: none;
  border-radius: 6px;
  cursor: pointer;
  transition: background-color 0.3s ease;
}

.coupon-btn:hover {
  background-color: #5a67d8;
}

.coupon-list {
  margin-top: 15px;
  border: 2px solid #e0e0e0;
  border-radius: 8px;
  max-height: 300px;
  overflow-y: auto;
}

.coupon-item {
  display: flex;
  align-items: center;
  padding: 20px;
  border-bottom: 1px solid #f0f0f0;
  cursor: pointer;
  transition: background-color 0.3s ease;
}

.coupon-item:last-child {
  border-bottom: none;
}

.coupon-item:hover {
  background-color: #f8f9fa;
}

.coupon-item-discount {
  font-size: 24px;
  font-weight: bold;
  color: #ff6b6b;
  margin-right: 20px;
  min-width: 60px;
}

.coupon-item-info {
  flex: 1;
}

.coupon-item-name {
  font-size: 16px;
  font-weight: bold;
  color: #333;
  margin-bottom: 5px;
}

.coupon-item-desc {
  font-size: 14px;
  color: #666;
  margin-bottom: 5px;
}

.coupon-item-expire {
  font-size: 12px;
  color: #999;
}

/* 订单摘要 */
.order-summary {
  background-color: #f8f9fa;
  padding: 25px;
  border-radius: 8px;
  margin-bottom: 30px;
}

.summary-item {
  display: flex;
  justify-content: space-between;
  margin-bottom: 15px;
  font-size: 16px;
  color: #666;
}

.discount {
  color: #ff6b6b;
}

.summary-total {
  display: flex;
  justify-content: space-between;
  font-size: 20px;
  font-weight: bold;
  color: #333;
  padding-top: 15px;
  border-top: 2px solid #e0e0e0;
}

.total-amount {
  color: #667eea;
  font-size: 24px;
}

/* 充值按钮 */
.recharge-btn {
  width: 100%;
  padding: 20px;
  background-color: #667eea;
  color: white;
  border: none;
  border-radius: 8px;
  font-size: 20px;
  font-weight: bold;
  cursor: pointer;
  transition: all 0.3s ease;
}

.recharge-btn:hover:not(:disabled) {
  background-color: #5a67d8;
  transform: translateY(-2px);
  box-shadow: 0 8px 25px rgba(102, 126, 234, 0.3);
}

.recharge-btn:disabled {
  background-color: #ccc;
  cursor: not-allowed;
}

/* 成功提示 */
.success-modal {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-color: rgba(0, 0, 0, 0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
}

.success-content {
  background-color: white;
  padding: 40px;
  border-radius: 12px;
  text-align: center;
  box-shadow: 0 10px 40px rgba(0, 0, 0, 0.2);
  max-width: 400px;
  width: 90%;
}

.success-icon {
  width: 80px;
  height: 80px;
  border-radius: 50%;
  background-color: #4caf50;
  color: white;
  font-size: 48px;
  display: flex;
  align-items: center;
  justify-content: center;
  margin: 0 auto 20px;
}

.success-content h3 {
  font-size: 24px;
  color: #333;
  margin-bottom: 15px;
}

.success-content p {
  font-size: 16px;
  color: #666;
  margin-bottom: 30px;
}

.success-btn {
  padding: 12px 30px;
  background-color: #667eea;
  color: white;
  border: none;
  border-radius: 6px;
  font-size: 16px;
  cursor: pointer;
  transition: background-color 0.3s ease;
}

.success-btn:hover {
  background-color: #5a67d8;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .recharge-content {
    padding: 20px;
  }
  
  .amount-options {
    grid-template-columns: repeat(3, 1fr);
  }
  
  .payment-options {
    grid-template-columns: 1fr;
  }
  
  .coupon-selector {
    flex-direction: column;
    align-items: flex-start;
  }
  
  .coupon-btn {
    margin-top: 15px;
    align-self: flex-end;
  }
}
</style>